package main.java.com.bookmanager.util;

import main.java.com.bookmanager.model.Book;
import main.java.com.bookmanager.model.User;
import main.java.com.bookmanager.service.BookService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class UserUtil {
    MemoryStorage storage = new MemoryStorage();
    List<User> users = new ArrayList<>();

    //创建新用户
    public boolean addUser(String username, String email, String password) {
        if (username.isEmpty() || email.isEmpty() || password.isEmpty()) {
            return false;
        }
        User user = new User(username, email, password);
        return users.add(user);
    }

    //借书
    public boolean lendBook(Book book,User user){
        if (!storage.borrow(book)){
            return false;
        }
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, +1);
        book.setReturnDate(cal.getTime());
        return user.getBooks().add(book);
    }

    //还书
    public boolean returnBook(Book book,User user){
        if (!storage.returnBook(book)){
            return false;
        }
        return user.getBooks().remove(book);
    }

    //验证登录
    public User verification(String email, String password){
        if (email.isEmpty() || password.isEmpty()) {
            return null;
        }
        for (User user : users) {
            if (user.getEmail().equals(email) && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

}
